package com.qf.ssm.service.impl;

import com.qf.ssm.common.exception.ServiceException;
import com.qf.ssm.common.request.MemberRequestParams;
import com.qf.ssm.common.vo.ResponseCode;
import com.qf.ssm.entity.Member;
import com.qf.ssm.mapper.MemberMapper;
import com.qf.ssm.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ISALYL
 * @create 2021-10-11 19:59
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;

    //显示收获地址
    @Override
    public String findAddress(Long memberId) {
        String address = memberMapper.selectAddress(memberId);
        if (address != null) {
            return address;
        } else {
            return null;
        }
    }

    //判断用户名和手机号是否存在
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public Boolean register(MemberRequestParams params) {
        List<Member> list = memberMapper.selectMember(params.getMemberName(), params.getMemberPhone());
        if (list == null || list.size() == 0) {
            Member member = new Member();
            // source  有值对象    target  目标对象
            BeanUtils.copyProperties(params, member);
            member.setPassword(DigestUtils.md5DigestAsHex(member.getPassword().getBytes()));
            int count = memberMapper.insert(member);
            if (count > 0) {
                return true;
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return false;
    }

    @Override
    public Integer modRemark(MemberRequestParams params) {
        Member member = new Member();
        BeanUtils.copyProperties(params,member);
        Integer remark  = memberMapper.updateRemark(member);
        if (remark != null) {
            return remark;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public Boolean deleteMember(Long memberId) {
        Integer delete = memberMapper.delMember(memberId);
        if (delete >0){
            return true;
        }else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }


}
